
import pygame
import random

# 定义游戏窗口大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# 初始化 pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("扫雷游戏")

# 定义方块类
class MineBlock:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.is_mine = False
        self.is_open = False
        self.neighbor_mines = 0

    def set_mine(self):
        self.is_mine = True

    def get_neighbor_mines(self):
        for i in range(self.x - 1, self.x + 2):
            for j in range(self.y - 1, self.y + 2):
                if i >= 0 and i < SCREEN_WIDTH and j >= 0 and j < SCREEN_HEIGHT:
                    block = grid[i][j]
                    if block.is_mine:
                        self.neighbor_mines += 1

# 定义网格类
class Grid:
    def __init__(self):
        self.width = SCREEN_WIDTH // 30
        self.height = SCREEN_HEIGHT // 30
        self.grid = []
        self.mine_count = 0
        self.open_cells = 0

        # 生成地雷
        while self.mine_count < 10:
            x = random.randint(0, SCREEN_WIDTH // 30 - 1) * 30
            y = random.randint(0, SCREEN_HEIGHT // 30 - 1) * 30
            if not self.is_mine_block(x, y):
                block = MineBlock(x, y)
                block.set_mine()
                self.mine_count += 1

        # 填充非地雷方块
        for i in range(SCREEN_WIDTH // 30):
            row = []
            for j in range(SCREEN_HEIGHT // 30):
                block = MineBlock(i * 30, j * 30)
                row.append(block)
            self.grid.append(row)

    def is_mine_block(self, x, y):
        for block in self.grid:
            for block in block:
                if block.x == x and block.y == y and block.is_mine:
                    return True
        return False

    def get_block(self, x, y):
        return self.grid[x // self.width][y // self.height]

    def open_cell(self, x, y):
        block = self.get_block(x, y)
        if not block.is_open:
            block.is_open = True
            self.open_cells += 1

            if block.is_mine:
                for block in self.grid:
                    for block in block:
                        block.is_open = False
            else:
                block.get_neighbor_mines()
                for i in range(block.x - 1, block.x + 2):
                    for j in range(block.y - 1, block.y + 2):
                        if i >= 0 and i < SCREEN_WIDTH and j >= 0 and j < SCREEN_HEIGHT:
                            neighbor_block = self.get_block(i, j)
                            if not neighbor_block.is_open:
                                self.open_cell(i, j)

# 创建网格对象
grid = Grid()

# 游戏循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP] and grid.open_cells > 0:
        x = random.randint(0, SCREEN_WIDTH // 30 - 1) * 30
        y = random.randint(0, SCREEN_HEIGHT // 30 - 1) * 30
        grid.open_cell(x, y)

    screen.fill((255, 255, 255))

    for i in range(SCREEN_WIDTH // 30):
        for j in range(SCREEN_HEIGHT // 30):
            block = grid.get_block(i, j)
            if block.is_mine:
                pygame.draw.rect(screen, (255, 0, 0), (i * 30, j * 30, 30, 30), 2)
            elif block.is_open:
                if block.neighbor_mines == 0:
                    pygame.draw.rect(screen, (0, 255, 0), (i * 30, j * 30, 30, 30), 2)
                else:
                    text = f"{block.neighbor_mines}"
                    font = pygame.font.Font(None, 20)
                    text_surface = font.render(text, True, (0, 0, 0))
                    screen.blit(text_surface, (i * 30 + 10, j * 30 + 10))
            else:
                pygame.draw.rect(screen, (128, 128, 128), (i * 30, j * 30, 30, 30), 2)

    pygame.display.flip()

pygame.quit()